﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Jack.DataScience.Data.Converters;
using Vav.Data.TransferTypes;
using Vav.Data.Utils;
using System.ComponentModel;
using CarPool.Data.Models;
using CarPool.Data.TransferTypes;
using System.Net.Http;
using Newtonsoft.Json;
using System.Threading.Tasks;

namespace Vav.Data.DataAccess
{
    public class CarPoolDataAccess
    {
        private readonly VavdbContext vavdbContext;

        public CarPoolDataAccess(VavdbContext vavdbContext)
        {
            this.vavdbContext = vavdbContext;
        }

        [Description("创建或者修改活动")]
        public int AddCarPool(SysCarpool sysCarpool)
        {
            vavdbContext.Add(sysCarpool);
            return vavdbContext.SaveChanges();
        }

        public List<SysCarpool> GetListCarPool(string keyword, int? type, int pageindex, int pagesize)
        {
            var query = vavdbContext.SysCarpool.Where(c => c.DataStatus == 0);
            if (!string.IsNullOrWhiteSpace(keyword))
                query = query.Where(c => c.LeaveAddress.Contains(keyword) || c.ArriveAddress.Contains(keyword));
            if (type.HasValue)
                query = query.Where(c => c.Type == type.Value);
            return query.OrderByDescending(c => c.CreateTime)
                .Skip((pageindex - 1) * pagesize)
                .Take(pagesize).ToList();

        }
        public SysCarpool GetCarPool(int? id)
        {
            if (id.HasValue)
            {
                return vavdbContext.SysCarpool.FirstOrDefault(c => c.Id == id);
            }
            return null;
        }

        public object GetListMyCarPool(int uid, int pageindex, int pagesize)
        {
            var query = vavdbContext.SysCarpool.Where(c => c.Uid == uid && c.DataStatus == 0);
            return query.OrderByDescending(c => c.CreateTime)
               .Skip((pageindex - 1) * pagesize)
               .Take(pagesize).ToList();
        }

        public async Task<int> AsyncData()
        {
            using (var client = new HttpClient() { BaseAddress = new Uri("http://tvapi.app6x.com/") })
            {
                //var json = JsonConvert.SerializeObject(new {or=0,lx=1 });
                var formData = new MultipartFormDataContent();
                formData.Add(new StringContent("0"), "or");
                formData.Add(new StringContent("1"), "lx");
                var response = await client.PostAsync($"douchang.php/index/Client/carpooling_li", formData);
                var responseJson = await response.Content.ReadAsStringAsync();
                var res = JsonConvert.DeserializeObject<List<AsyncDataModel>>(responseJson);
                var list = vavdbContext.SysCarpool.ToList();
                List<AsyncDataModel> temp = new List<AsyncDataModel>();
                foreach (var r in res)
                {
                    var flag = list.Any(d => d.LeaveAddress == r.chufa && d.ArriveAddress == r.mudi && d.Phone == r.tel);
                    if (!flag)
                    {
                        temp.Add(r);
                    }
                }
                List<SysCarpool> carpools = new List<SysCarpool>();
                foreach (var item in temp)
                {
                    carpools.Add(new SysCarpool
                    {
                        Uid = 13,
                        Type = 2,
                        LeaveAddress = item.chufa,
                        LeaveTime = Convert.ToDateTime(item.cf_time),
                        ArriveAddress = item.mudi,
                        Remark = item.conter,
                        Person = item.renshu,
                        ContactName = "",
                        Phone = item.tel,
                        CreateTime = Convert.ToDateTime(item.update_time),
                    });
                }
                vavdbContext.AddRange(carpools);
                return vavdbContext.SaveChanges();
            }
        }
        public async Task<int> AsyncDataCar()
        {
            using (var client = new HttpClient() { BaseAddress = new Uri("http://tvapi.app6x.com/") })
            {
                //var json = JsonConvert.SerializeObject(new {or=0,lx=1 });
                var formData = new MultipartFormDataContent();
                formData.Add(new StringContent("0"), "or");
                formData.Add(new StringContent("0"), "lx");
                var response = await client.PostAsync($"douchang.php/index/Client/carpooling_li", formData);
                var responseJson = await response.Content.ReadAsStringAsync();
                var res = JsonConvert.DeserializeObject<List<AsyncDataModel>>(responseJson);
                var list = vavdbContext.SysCarpool.ToList();
                List<AsyncDataModel> temp = new List<AsyncDataModel>();
                foreach (var r in res)
                {
                    var flag = list.Any(d => d.LeaveAddress == r.chufa && d.ArriveAddress == r.mudi && d.Phone == r.tel);
                    if (!flag)
                    {
                        temp.Add(r);
                    }
                }
                List<SysCarpool> carpools = new List<SysCarpool>();
                foreach (var item in temp)
                {
                    carpools.Add(new SysCarpool
                    {
                        Uid = 13,
                        Type = 1,
                        LeaveAddress = item.chufa,
                        LeaveTime = Convert.ToDateTime(item.cf_time),
                        ArriveAddress = item.mudi,
                        Remark = item.conter,
                        Person = item.renshu,
                        ContactName = "",
                        Phone = item.tel,
                        CreateTime = Convert.ToDateTime(item.update_time),
                    });
                }
                vavdbContext.AddRange(carpools);
                return vavdbContext.SaveChanges();
            }
        }
    }
}
